package hiatao.puzzle.utils;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

import haitao.puzzle.R;

/**
 * 工具类
 * 
 * @author yzx
 * 
 */
public class Tools {
	private static Context context;
	private static Dialog ad;
	private static TextView tv;
	private static Toast to;
	/**
	 * 线程池最大并发数
	 */
	public static final int THREAD_POOL_MAX = 15;

	public static final int CAMERA = 1;
	public static final int PICLIB = 2;
	public static final int CUT = 3;
	/**
	 * 初始化注入Context对象
	 */
	public static void setContext(Context context) {
		Tools.context = context;
	}
	/**
	 * 显示Toast
	 *
	 * @param str
	 *            要显示的内容
	 */
	public static void showToast(final String str) {
		if (str == null)
			return;
		if (to == null)
			to = Toast.makeText(context, str, 600);
		else
			to.setText(str);
		to.show();
	}
	/**
	 * log (user error)
	 *
	 * @param str
	 */
	public static void log(String str) {
		if (str != null)
			Log.e("--------->>", str);
	}
	/**
	 * 显示Toast
	 *
	 * @param str
	 *            要显示的内容
	 */
	public static void showToast(int strId) {

		if (to == null)
			to = Toast.makeText(context, strId, Toast.LENGTH_SHORT);
		else
			to.setText(strId);
		to.show();
	}

	/**
	 * 使用SharedPreferences存储值
	 *
	 * @param key
	 *            存储值的key
	 * @param value
	 *            存储值的value
	 *            文件名称
	 */
	public static void saveValues(String key, String value) {
		context.getSharedPreferences("default_puzzle_sp", Context.MODE_PRIVATE).edit().putString(key, value).commit();
	}

	/**
	 * 使用SharedPreferences取值
	 *
	 * @param key
	 *            key
	 *            xml文件名称
	 * @return 如果值不存在 ,返回空串
	 */
	public static String getValues(String key) {
		return context.getSharedPreferences("default_puzzle_sp", Context.MODE_PRIVATE).getString(key, "");
	}

	/**
	 * 线程池
	 *
	 * @param run
	 */
	public static void exec(Runnable run) {
		new Thread(run).start();
	}
	/**
	 * @param context
	 * @param view
	 * @param x 显示的x坐标
	 * @param y 显示的y坐标
	 * @param width dialog的宽度
	 * @param height  dialog的高度
	 * @param animation 显示和消失的动画效果
	 * @param cancleAble 是否可以cancle掉
	 * @param isAnimation
	 * @return
	 */
	private static Dialog showDialog(Context context, View view, int x, int y, int width, int height, int animation, boolean cancleAble, boolean isAnimation) {
		Dialog ad;
		if ( Build.VERSION.SDK_INT >=11){
			ad = new AlertDialog.Builder(context, R.style.alert_dialog).create();
		}else{
			ad = new Dialog(context,R.style.alert_dialog);
		}
		ad.setCancelable(cancleAble);
		WindowManager.LayoutParams lp = ad.getWindow().getAttributes();
		lp.x = x;
		lp.y = y;
		ad.show();
		ad.setContentView(view);
		ad.getWindow().setLayout(width, height);
	/*	if (isAnimation) {
			if (animation != 0)
				ad.getWindow().setWindowAnimations(animation);
			else
				ad.getWindow().setWindowAnimations(R.style.head_in_out);
		}*/
		ad.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		return ad;
	}
	/**
	 * 弹出提示用户等待的dialog
	 */
	public static Dialog showDialog(final Context context) {
		showWaitDialog(context, "请稍等", false);
		return ad;
	}
	/**
	 * 弹出提示用户等待的dialog
	 */
	public static void showWaitDialog(final Context context, String str) {
		showWaitDialog(context, str, false);
	}

	public static float getDimension(int id){
		if(id==0){
			return 0;
		}else{
			return context.getResources().getDimension(id);
		}
	}
	/**
	 * 弹出提示用户等待的dialog
	 */
	public static void showWaitDialog(final Context context, String str,boolean canCancle) {
		if (ad==null || !ad.isShowing() ) {
			View vv = View.inflate(context, R.layout.dialog_wait, null);
			tv = (TextView) vv.findViewById(R.id.tv);
			if (!Tools.isEmptyStr(str)) {
				tv.setText(str);
			}
			ad = showDialog(context, vv, 0, 0, (int)(Tools.getsx() / 1.8),  (int)(Tools.getsy() / 6.5), 0,canCancle, false);
		}else{
			if (!Tools.isEmptyStr(str)) {
				tv.setText(str);
			}
			ad.setCancelable(canCancle);
		}
	}
	public static boolean isEmptyStr(String str) {
		return (str == null || str.trim().length() < 1);
	}
	public static int getsx() {
		return context.getResources().getDisplayMetrics().widthPixels;
	}

	public static int getsy() {
		int px = Resources.getSystem().getDimensionPixelSize(Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
		return context.getResources().getDisplayMetrics().heightPixels - px;
	}
	/**
	 * 取消等待的dialog
	 */
	public static void dismissWaitDialog() {
		if (ad != null)
			ad.dismiss();
	}
	/**
	 * 获取视频缩略图  可能为null 用前先判断
	 * @param videoPath
	 * @param width
	 * @param height
	 * @return
	 */
	public static Bitmap getVideoThumbnail(String videoPath, int width, int height) {
		Bitmap bitmap = null;
		// 获取视频的缩略图
		bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.MICRO_KIND);
		System.out.println("w"+bitmap.getWidth());
		System.out.println("h"+bitmap.getHeight());
		bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,  ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
		return bitmap;
	}
	private static Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			if (msg.obj != null) {
				Runnable r = (Runnable) msg.obj;
				r.run();
			}
		};
	};
	/**
	 * 主线程回到使用
	 */
	public static void runOnUI(Runnable run) {
		Message me = handler.obtainMessage();
		me.obj = run;
		handler.sendMessage(me);
	}
	/**
	 * 启动一个新的Activity
	 *
	 * @param from
	 *            起始Activity
	 * @param to
	 *            跳转Activity
	 */
	public static void goActivity(Context from, Class<? extends Activity> to, String[] keys, String[] values) {
		Intent in = new Intent(from, to);
		if (keys != null && values != null)
			for (int i = 0; i < keys.length; i++)
				in.putExtra(keys[i], values[i]);
		from.startActivity(in);
	}
	/**
	 * 获取随机数
	 * @param min
	 * @param max
	 * @return
	 */
	public static int getRandom(int min,int max){
		Random random = new Random();
		return random.nextInt(max)%(max-min+1) + min;
	}
	private static Runnable run;
	private static int delaytime;
	public static void startTask(Runnable _run,int _delaytime){
		run=_run;
		delaytime =_delaytime;
		handler2.postDelayed(runnable,0);
		// 开始Timer
	}
	public static void finishTask(){
		handler2.removeCallbacks(runnable);           //停止Timer
		run=null;// 提醒回收
	}
	private static Handler handler2 = new Handler( );

	private static Runnable runnable = new Runnable( ) {

		public void run ( ) {
			if (run!=null) {
				run.run();
			}
			handler2.postDelayed(this,delaytime);     //postDelayed(this,1000)方法安排一个Runnable对象到主线程队列中
		}
	};



}
